﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace MapSurface.WidgetsProperties
{
    public class ParseValue
    {
        public Double parseStrToDouble(string value)
        {
            if (IsDouble(value))
                return Double.Parse(value);
            else
                return -1;
        }

        public bool IsDouble(string str)
        {
           try
            {
                double d = Double.Parse(str);
            }
            catch (FormatException )
            {
                return false;
            }
            catch (Exception )
            {
                return false;
            }
            return true;
        }
        public bool isMargin(string value)
        {
           string[] marg=value.Split(',');
           if (marg != null && marg.Length == 4)
               return true;
           return false;
        }
        public Thickness parseToMargin(string value)
        {
            Thickness tk = new Thickness(0, 0, 0, 0);
            if (isMargin(value))
            {
                string[] marg = value.Split(',');
                tk = new Thickness(parseStrToDouble(marg[0]), parseStrToDouble(marg[1]),parseStrToDouble( marg[2]),parseStrToDouble( marg[3]));
            }
            return tk;
        }
        public Boolean isBoolean(string value)
        {
            if (value.CompareTo("False") == 0 || value.CompareTo("True")==0)
                return true;
            else
                return false;
        }
        public Boolean parseToBoolean(string value)
        {
            if (value.CompareTo("False") == 0)
                return false;
            else
                return true;
        }
        public Visibility parseToVisibility(string value)
        {
            Visibility v=Visibility.Visible;
            if (value.CompareTo("Hidden")==0)
                v = Visibility.Hidden;
            else if (value.CompareTo("Collapsed")==0)
                v = Visibility.Collapsed;
            return v;
        }

        public bool isBrush(string value)
        {
            try
            {
                Type t = typeof(Brushes);
                Brush b = (Brush)t.GetProperty(value).GetValue(null, null);
                return true;
            }
            catch (Exception e)
            {
                return false;

            }
            
        }
        public Brush  parseToBrush(String value)
        {
            try{

                Type t = typeof(Brushes);
                Brush b = (Brush)t.GetProperty(value).GetValue(null, null);
                return b;
            }catch (Exception e)
            {
                return null;
            }
          
        }
    }
}
